package com.jh.mytext2;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 * 默认弹框
 * @author jh
 *
 */
public class DefaultDialog extends AlertDialog {

    private int kfbLayoutResID = R.layout.dialog_activity;// 布局文件

    public DefaultDialog(Context context, int theme, int kfbLayoutResID) {
        super(context, theme);
        this.kfbLayoutResID=kfbLayoutResID;

    }

    private Button button1, button2,btClose;
    private TextView tvText1;
    private String btText1, btText2,text1;

    private boolean isHide=false;


    private View.OnTouchListener touch1, touch2;
    private View.OnClickListener close;

    public View.OnTouchListener getTouch1() {
        return touch1;
    }

    public void setTouch1(View.OnTouchListener touch1) {
        this.touch1 = touch1;
    }

    public View.OnTouchListener getTouch2() {
        return touch2;
    }

    public void setTouch2(View.OnTouchListener touch2) {
        this.touch2 = touch2;
    }

    public View.OnClickListener getClose() {
        return close;
    }

    public void setClose(View.OnClickListener close) {
        this.close = close;
    }

    public DefaultDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(kfbLayoutResID);
        setCanceledOnTouchOutside(false);

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        btClose = (Button) findViewById(R.id.btClose);
        tvText1= (TextView) findViewById(R.id.text1);


        if (button1 != null && touch1 != null) {

            button1.setOnTouchListener(touch1);
        }
        if (button2 != null && touch2 != null) {

            button2.setOnTouchListener(touch2);
        }
        if (btClose!=null&&close!=null) {
            btClose.setOnClickListener(close);

        }

        if (btText1 != null&&button1 !=null) {
            button1.setText(btText1);
        }
        if (btText2 != null&&button2!=null) {
            button2.setText(btText2);
        }
        if (tvText1!=null && text1!=null) {

            tvText1.setText(text1);

        }

        if (isHide && button1!=null && button2!=null) {

            button1.setVisibility(View.GONE);
            button2.setVisibility(View.GONE);

        }



    }



    public void setButton1(String text) {
        btText1 = text;
        if (button1 != null) {

            button1.setText(btText1);
        }
    }

    public void setButton2(String text) {
        btText2 = text;
        if (button2!=null) {
            button2.setText(btText2);
        }

    }

    public void setTvText1(String text){
        text1=text;
        if (tvText1!=null) {
            tvText1.setText(text1);

        }
    }

    public Button getButton(){
        return button2;
    }
    public Button getButton1(){
        return button1;
    }

    public Button getBtClose(){
        return btClose;
    }

    public void hideButton(){
        if (button1!=null && button2!=null && btClose!=null) {
            button1.setVisibility(View.INVISIBLE);
            button2.setVisibility(View.INVISIBLE);

        }
    }
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // 拦截返回键，不关闭对话框
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

}
